package com.myqm.util.gateway;
 
import java.util.ArrayList;
import java.util.List;
import com.alibaba.fastjson.JSON; 

public class ApiGateway {
	static String url ="http://222.196.35.4:8001";
	
	public static ServicesList getServices(String nameOrId) throws Exception {
		String path = url + "/services";
		if (nameOrId != null)
			path += "/" + nameOrId;
		String result = ""; 
		System.out.println(path);
		result = ApiGatewayNetOp.netByGET(path, null); 
		ServicesList list =  JSON.parseObject(result,ServicesList.class);
		System.out.println(result);
		return list; 
	} 
	
 

	public static ApiInfo getApi(String nameOrId) throws Exception {
		String path = url + "/apis";
		if (nameOrId != null)
			path += "/" + nameOrId;
		String result = ""; 
		System.out.println(path);
		result = ApiGatewayNetOp.netByGET(path, null); 
		ApiInfo api =  JSON.parseObject(result,ApiInfo.class);
		System.out.println(result);
		return api; 
	} 
	 
	public static ApiInfo addApi(ApiInfo api) throws Exception {
		String path = url + "/apis";
		api.setName("mail");
		List<String> urls = new ArrayList<String>();
		urls.add("/temp");
		api.setUris(urls);
		api.setUpstream_url("http://mail.qq.com");
		List<String> methodsParam = new ArrayList<String>();
		
		methodsParam.add("POST");
		api.setMethods(methodsParam);
	 
		String result = ""; 
		System.out.println(path);
		result = ApiGatewayNetOp.netNotByGET(path, api, "POST"); 
		ApiInfo newapi =  JSON.parseObject(result, ApiInfo.class);
		System.out.println(newapi);
		return api; 
	} 
	
	public static ApisList getApis() throws Exception {
		String path = url + "/apis";
		String result = ""; 
		System.out.println(path);
		result = ApiGatewayNetOp.netByGET(path, null); 
		ApisList list =  JSON.parseObject(result,ApisList.class);
		System.out.println(result);
		return list; 
	} 
	
	public static ApiInfo delApi(String nameOrId) throws Exception {
		String path = url + "/apis";
		if(nameOrId != null)
			path += "/" + nameOrId;
		String result = "";
		System.out.println(path);
		result = ApiGatewayNetOp.netNotByGET(path, null, "DELETE");
		ApiInfo api = JSON.parseObject(result, ApiInfo.class);
		System.out.println(api);
		return api;
	}
   
	public static void main(String[] args) {
	   try {
		   //ApiGateway.addApi(new ApiInfo());
		   //ApiGateway.delApi("mail");
		   ApiGateway.getApi("qq");
	   } catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println(e.getMessage());
	   }
   }
}
